package light.head.job;

import java.util.Date;
import java.util.List;


import light.head.dao.site.NewsDAO;
import light.head.dao.site.NewsItemDAO;
import light.head.model.News;
import light.head.model.NewsItem;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SpiderJob {
	private NewsDAO newsDAO;
	private NewsItemDAO itemDAO;
	
	public void execute(){
		
		List<News> newsList = newsDAO.find();
		for (News news : newsList) {
			News ns = new News();
			ns.setCt(new Date());
			ns.setDeleted(false);
			ns.setContent(news.getContent());
			ns.setK(news.getK());
			ns.setK1(news.getK1());
			ns.setStarturl(news.getStarturl());
			ns.setT(news.getT());
			ns.setV(news.getV());
			ns.setV1(news.getV1());
			ns.setWww(news.getWww());
			newsDAO.add(ns);
			if (news.getStarturl() != null && news.getWww() != null && news.getT() != null && news.getK() != null && news.getV() != null 
					&& news.getContent() != null && news.getK1() != null && news.getV1() != null){
				try {
					// 查询页面新闻的  标签 以及ID
					NodeFilter filter = new AndFilter(new TagNameFilter(news.getT()), new HasAttributeFilter(news.getK(), news.getV()));
					Parser parser = new Parser();
					Parser bodyparser = new Parser();
					parser.setURL(news.getStarturl());
					parser.setEncoding(parser.getEncoding());
					//获取匹配的fileter的节点
					NodeList list = parser.extractAllNodesThatMatch(filter);
					for (int i = 0; i < list.size(); i++) {
						Tag node = (Tag) list.elementAt(i);
						for (int j = 0; j < node.getChildren().size(); j++) {
							String textstr = node.getChildren().elementAt(j).toHtml().trim();
							if (textstr.length() > 0) {
								int linkbegin=0,linkend=0,titlebegin=0,titleend=0;
								while(true){
									textstr = textstr.replaceAll("\'","\"");
									linkbegin = textstr.indexOf("href=\"",titleend);// 截取链接字符串起始位置
									//如果不存在 href了 也就结束了
									if(linkbegin <= 0) break;
									linkend = textstr.indexOf("\"",linkbegin + 6);// 截取链接字符串结束位置
									
									String sublink = textstr.substring(linkbegin + 6,linkend);
									String link = news.getWww()+sublink;
									titlebegin = textstr.indexOf("\">",linkend);
									titleend = textstr.indexOf("</a>",titlebegin);
									String title = textstr.substring(titlebegin + 2,titleend);
									System.out.println("正在抓取 " + title);
									try {
										//匹配正文有DIV
										NodeFilter bodyfilter = new AndFilter( new TagNameFilter(news.getContent()), new HasAttributeFilter(news.getK1(), news.getV1()));
										bodyparser.setURL(link);
										bodyparser.setEncoding(bodyparser.getEncoding());
										NodeList bodylist = bodyparser.extractAllNodesThatMatch(bodyfilter);
										//将DIV中的标签都 去掉只留正文
										String newstextstr = bodylist.asString();
										List<NewsItem> itemList = newsDAO.findAll(link);
										if (itemList == null || itemList.size() == 0) {
											NewsItem item = new NewsItem();
											item.setUc(ns.getCode());
											item.setContent(newstextstr);
											item.setDeleted(false);
											item.setTitle(title);
											item.setUrl(link);
											itemDAO.add(item);
										}
									} catch (Exception e) {
										System.out.println("抓取信息子页面出错，出错信息为：");
										e.printStackTrace();
									}
								}
							}
						}
					}
				} catch (Exception e) {
					System.out.println("抓取信息出错，出错信息为：");
					e.printStackTrace();
					return;
				}
			}
		}		
		
	}
	@Autowired
	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}
	@Autowired
	public void setItemDAO(NewsItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	
}
